package com.mcxx.modules.minimumliving.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mcxx.common.exception.BizException;
import com.mcxx.constant.MQConstant;
import com.mcxx.modules.common.service.CheckStartService;
import com.mcxx.modules.common.service.impl.IntegrateCheckServiceImpl;
import com.mcxx.modules.minimumliving.service.ScheduleCheckService;
import com.mcxx.modules.miniSalvation.check.entity.CheckEntity;
import com.mcxx.modules.miniSalvation.check.service.read.CheckReadService;
import com.mcxx.modules.miniSalvation.check.service.write.CheckWriteService;
import com.mcxx.modules.miniSalvation.order.dto.CheckDTO;
import com.mcxx.modules.system.dto.MQMessageDTO;
import com.mcxx.util.Constant;
import com.mcxx.util.ScheduleRequestUtil;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@TaTransactional
public class ScheduleCheckServiceImpl extends BaseService implements ScheduleCheckService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private CheckStartService checkStartService;
    @Autowired
    private CheckReadService checkReadService;
    @Autowired
    private IntegrateCheckServiceImpl integrateCheckService;
    private static final Logger logger = LoggerFactory.getLogger(ScheduleRequestUtil.class);



    @Override
    public String sendCheckInfo(List<CheckDTO> checkDTOList, String businessCode, UserAccountVo accountVo,String checkType){

        String resMsg = "";
        try {


           String areaCode = accountVo.getAreaCode();
           String countyCode = areaCode.substring(0,6);
           if(Arrays.stream(CheckWriteService.IntegrateAreaCode).filter(o->o.equals(countyCode)).findAny().isPresent()){
               resMsg = integrateCheckService.startCheck(checkDTOList,businessCode,areaCode,accountVo,checkType);
           }else{
               resMsg =  checkStartService.startCheck(JSONObject.toJSONString(checkDTOList),businessCode,areaCode);
           }
        }catch (Exception e){
            logger.error("核对异常"+e);
            throw new BizException("-1","核对发生异常");
        }
        return resMsg;
    }


    @Override
    public void sendApproveResult(List <String> orderIds,String businessCode,UserAccountVo accountVo){

        String countyCode = accountVo.getAreaCode().substring(0,6);
        if(Arrays.stream(CheckWriteService.IntegrateAreaCode).filter(o->o.equals(countyCode)).findAny().isPresent()){
            List<CheckEntity>  entitys = checkReadService.selectUnFinishCheckByOrderId(orderIds.get(0));
            if(null == entitys || entitys.size()==0){
                return;
            }
            for(CheckEntity entity:entitys){
                integrateCheckService.repealCheck(entity,accountVo);
            }
        }

    }


    @Override
    public void repealUnfinishCheck(String orderId,UserAccountVo accountVo){
        List<CheckEntity>  entitys = checkReadService.selectUnFinishCheckByOrderId(orderId);
        if(null == entitys || entitys.size()==0){
            return;
        }

        String countyCode = accountVo.getAreaCode().substring(0,6);
        if(Arrays.stream(CheckWriteService.IntegrateAreaCode).filter(o->o.equals(countyCode)).findAny().isPresent()){
            for(CheckEntity entity:entitys){
                integrateCheckService.repealCheck(entity,accountVo);
            }
        }
    }
}
